//
//  YRAppPrivate
//  TestFont
//
//  Created by 薛永伟 on 2019/9/6.
//  Copyright © 2019 XueYongWei. All rights reserved.
//

import UIKit

class YRAppPrivate{
    
    private static let shared = YRAppPrivate()
    
    lazy var appCorver: YRAppPrivate.CoverView = {
        let view = YRAppPrivate.CoverView()
        view.frame = UIScreen.main.bounds
        return view
    }()
    
    class func setup(){
        
        shared.addNotifacationObser()
    }
    
    private func addNotifacationObser(){
        
        NotificationCenter.default.addObserver(self, selector: #selector(showCorver), name: UIApplication.didEnterBackgroundNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(hiddenCorver), name: UIApplication.didBecomeActiveNotification, object: nil)
        
    }
    
    @objc private  func showCorver(){
        
        UIApplication.shared.keyWindow?.addSubview(appCorver)
    }
    
    @objc private func hiddenCorver(){
        
        appCorver.removeFromSuperview()
    }
}

//MARK: - --------------CoverView--------------

extension YRAppPrivate {
    
    class CoverView:UIView{
        
        lazy var visualEffectView: UIVisualEffectView = {
            let effect = UIBlurEffect.init(style: .dark)
            let view = UIVisualEffectView.init(effect: effect)
            view.frame = UIScreen.main.bounds
            return view
        }()
        
        lazy var tipLabel: UILabel = {
            let view = UILabel()
            view.textColor = UIColor.lightText
            view.textAlignment = .center
            view.font = UIFont.systemFont(ofSize: 16, weight: .regular)
            view.text = "为您的隐私保驾护航"
            return view
        }()
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupViews()
        }
        
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setupViews()
        }
        
        func setupViews(){
            
            addSubview(visualEffectView)
            visualEffectView.translatesAutoresizingMaskIntoConstraints = false
            visualEffectView.topAnchor.constraint(equalTo: topAnchor).isActive = true
            visualEffectView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
            visualEffectView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
            visualEffectView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
            
            addSubview(tipLabel)
            tipLabel.translatesAutoresizingMaskIntoConstraints = false
            tipLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
            tipLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 30).isActive = true
        }
    }
}
